package com.anxinr.service.impl;

import com.anxinr.constant.RedisConstant;
import com.anxinr.enums.ExceptionEnum;
import com.anxinr.exception.LogicException;
import com.anxinr.mapper.UserMapper;
import com.anxinr.pojo.dto.UserDTO;
import com.anxinr.pojo.entity.User;
import com.anxinr.service.UserService;
import com.anxinr.util.JWTUtils;
import com.anxinr.util.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author anxinr
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper ;
    @Autowired
    private RedisUtil redisUtil ;

    /**
     * 用户登录
     * @author anxinr
     * @return HashMap
     * @param user
     */
    @Override
    public HashMap<String, Object> login(UserDTO user) {
        /**返回token信息*/
        HashMap<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,user.getUserName());
        /**查找用户*/
        User one = this.getOne(wrapper);
        /**用户不存在*/
        if (Objects.isNull(one)){
            throw new LogicException(ExceptionEnum.STUDY_USER_NAME_ERROR) ;
        }
        /**密码错误*/
        if (!one.getPassword().equals(user.getPassword())){
            throw new LogicException(ExceptionEnum.STUDY_LOGIN_ERROR) ;
        }
        /**缓存用户信息*/
        BeanUtils.copyProperties(one,user);
        user.setToken(JWTUtils.generate(user.getId()+""));
        redisUtil.setUserInfo(RedisConstant.USER_TOKEN_INFO + user.getToken(), user, TimeUnit.MINUTES ,RedisConstant.USER_TOKEN_INFO_TIME);
        /**返回token以及用户信息*/
        map.put("token",user.getToken()) ;
        map.put("user",user);
        return map;
    }

    /**
     * 用户注册
     * @param user
     */
    @Override
    public void register(UserDTO user) {
        if (Objects.isNull(user)){
            throw new LogicException("注册信息不能为空！") ;
        }
        User one = null ;
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,user.getUserName());
        /**查看用户是否存在*/
        try {
            one = this.getOne(wrapper);
        }catch (Exception e){
            throw new LogicException(ExceptionEnum.ERROR_MASSAGE) ;
        }
        if (!Objects.isNull(one)){
            throw new LogicException(ExceptionEnum.STUDY_USER_REPEAT_ERROR) ;
        }
        if (Objects.isNull(user.getPassword())){
            throw new LogicException(ExceptionEnum.STUDY_USER_REPEAT_ERROR) ;
        }
        if (Objects.isNull(user.getRealName())){
            throw new LogicException("真实名称不能为空！") ;
        }
        if (Objects.isNull(user.getPhone())){
            throw new LogicException("手机号码不能为空！") ;
        }
        if (Objects.isNull(user.getUserName())){
            throw new LogicException("登录账号不能为空！") ;
        }
        /**没有异常情况下，完成用户的注册*/
        User userInfo = new User();
        BeanUtils.copyProperties(user,userInfo);
        this.save(userInfo) ;
    }

    /**
     * 管理员登录
     * @author anxinr
     * @return HashMap
     */
    @Override
    public HashMap<String, Object> loginAdmin(UserDTO user) {
        /**返回token信息*/
        HashMap<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName,user.getUserName());
        /**查找用户*/
        User one = this.getOne(wrapper);
        /**用户不存在*/
        if (Objects.isNull(one)){
            throw new LogicException(ExceptionEnum.STUDY_USER_NAME_ERROR) ;
        }
        /**密码错误*/
        if (!one.getPassword().equals(user.getPassword())){
            throw new LogicException(ExceptionEnum.STUDY_LOGIN_ERROR) ;
        }
        /**检查是否是管理员*/
        if (!Integer.valueOf(1).equals(one.getIsAdmin())){
            throw new LogicException("非管理员不能登录！") ;
        }
        /**缓存用户信息*/
        BeanUtils.copyProperties(one,user);
        user.setToken(JWTUtils.generate(user.getId()+""));
        redisUtil.setUserInfo(RedisConstant.USER_TOKEN_INFO + user.getToken(), user, TimeUnit.MINUTES ,RedisConstant.USER_TOKEN_INFO_TIME);
        /**返回token以及用户信息*/
        map.put("token",user.getToken()) ;
        map.put("user",user);
        return map;
    }
}
